<html>
<head><meta charset="utf-8"><title>FromStr for &amp;str · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/FromStr.20for.20.26str.html">FromStr for &amp;str</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="201692864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/FromStr%20for%20%26str/near/201692864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/FromStr.20for.20.26str.html#201692864">(Jun 23 2020 at 06:58)</a>:</h4>
<p>Is there a reason why FromStr is not implemented for &amp;str?</p>



<a name="201693018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/FromStr%20for%20%26str/near/201693018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Peter Rabbit <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/FromStr.20for.20.26str.html#201693018">(Jun 23 2020 at 07:00)</a>:</h4>
<p><span class="user-mention silent" data-user-id="247690">chabing</span> <a href="#narrow/stream/122651-general/topic/FromStr.20for.20.26str/near/201692864">said</a>:</p>
<blockquote>
<p>Is there a reason why FromStr is not implemented for &amp;str?</p>
</blockquote>
<p>Because the return value of <code>FromStr::from_str</code> cannot use the lifetime of the <code>&amp;str</code> that was passed in. Hence why <code>FromStr</code> is implemented for <code>String</code> but not for <code>&amp;str</code>.</p>



<a name="201694224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/FromStr%20for%20%26str/near/201694224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/FromStr.20for.20.26str.html#201694224">(Jun 23 2020 at 07:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125267">Peter Rabbit</span> <a href="#narrow/stream/122651-general/topic/FromStr.20for.20.26str/near/201693018">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="247690">chabing</span> <a href="#narrow/stream/122651-general/topic/FromStr.20for.20.26str/near/201692864">said</a>:</p>
<blockquote>
<p>Is there a reason why FromStr is not implemented for &amp;str?</p>
</blockquote>
<p>Because the return value of <code>FromStr::from_str</code> cannot use the lifetime of the <code>&amp;str</code> that was passed in. Hence why <code>FromStr</code> is implemented for <code>String</code> but not for <code>&amp;str</code>.</p>
</blockquote>
<p>So what would be the best way to do something like this (contrived) example:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">std</span>::<span class="kt">str</span>::<span class="n">FromStr</span><span class="o">&gt;</span><span class="p">(</span><span class="n">s</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">T</span>::<span class="n">from_str</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Ok</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="nb">Err</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">panic</span><span class="o">!</span><span class="p">(),</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="s">&quot;3&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span>: <span class="kp">&amp;</span><span class="kt">str</span> <span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="s">&quot;abc&quot;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="201721717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/FromStr%20for%20%26str/near/201721717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/FromStr.20for.20.26str.html#201721717">(Jun 23 2020 at 13:02)</a>:</h4>
<p>I'd implement your own trait, following the example of <code>TryFrom</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>